package Experiment;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

public class CarList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html,charset=UTF-8");

        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Map<String, Integer> car = (Map<String, Integer>) session.getAttribute("shoppingCar");
        if (car != null && car.size() > 0) {
            out.println("your car list:<br>");
            //遍历显示购物车里面的商品
            for (String bookname : car.keySet()) {
                out.println(bookname + "," + car.get(bookname) + "<br>");
            }
        } else {
            out.println("no goods+<br>");
        }
        out.println("<a href = 'http://localhost:8080/javaWeb_war_exploded/BookChoose.jsp'>keep going");
    }
}
